package com.example.yixueguan.activity;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;
import java.util.Map;

import com.example.yixueguan.R;
import com.example.yixueguan.bean.StudentsDAO;
/**
 * 查询学生信息
 */
public class QueryActivity extends AppCompatActivity {
    //定义组件
    ListView listView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_books);
        //初始化界面
        initView();
    }

    private void initView() {
        //建立数据库访问对象
        StudentsDAO dao = new StudentsDAO(getApplicationContext());
        //打开数据库
        dao.open();
        //调用数据库访问方法
        List<Map<String,Object>> mOrderData = dao.getAllstudents();
        if (mOrderData == null) return;
        //获取组件
        listView=(ListView)findViewById(R.id.list_students);
        //定义数据源
        String[] from = {"studentid", "studentname", "majoy", "studentclass"};
        //定义布局控件ID
        int[] to = {R.id.tv_lst_studentid, R.id.tv_lst_studentname, R.id.tv_lst_majoy, R.id.tv_lst_studentclass};
        SimpleAdapter listItemAdapter = new SimpleAdapter(QueryActivity.this, mOrderData, R.layout.item_list, from, to);
        //添加并显示
        listView.setAdapter(listItemAdapter);
        //关闭数据库
        dao.close();
    }
}
